package com.example.boot;

import java.util.UUID;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import com.example.boot.dao.UserRepository;
import com.example.boot.entity.User;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

/**
 * 
 * @author KL 
 * @date  2018年3月3日
 * @description 模拟2条用户数据
 *
 */
@Component
@Slf4j
class DataInitializer implements CommandLineRunner {

	private final UserRepository userRepository;

	public DataInitializer(UserRepository userRepository) {
		this.userRepository = userRepository;
	}

	@Override
	public void run(String[] args) {
		log.info("数据初始化 ...");
		this.userRepository
		.deleteAll()
		.thenMany(
				Flux
				.just("User one", "User two")
				.flatMap(
						name -> {
							String id = UUID.randomUUID().toString();
							return this.userRepository.save(User.builder().id(id).name(name).age(name+" 20").build());
						}
						)
				)
		.log()
		.subscribe(
				null,
				null,
				() -> log.info("数据初始化结束...")
				);

	}

}
